package org.lq.er.view.announcement;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.lq.er.entity.Announcement;
import org.lq.er.service.impl.AnnouncementServiceImpl;

/**
 * @author  张鹤山
 *
 */
public class AnnouncementAdd extends JDialog implements FocusListener, KeyListener{

	private JPanel contentPane;
	private JTextArea textArea;
	private JButton button;
	private JLabel label;
	private static Admin_announcement admin;
	private boolean update = false;
	private int id;
	private AnnouncementServiceImpl as = new AnnouncementServiceImpl();;
	private JScrollPane scrollPane;
	private int len;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AnnouncementAdd frame = new AnnouncementAdd();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AnnouncementAdd() {
		super(admin, true);
		setTitle("公告");
		setBounds(100, 100, 450, 300);
		setLocationRelativeTo(null);
		setResizable(false);
		contentPane = new JPanel(null);
		contentPane.setBackground(Color.white);
		setContentPane(contentPane);
		
		JButton empty = new JButton();
		empty.setBounds(0,0,0,0);
		contentPane.add(empty);
		empty.requestFocus();
		
		label = new JLabel("0/255");
		label.setFont(new Font("宋体", Font.PLAIN, 15));
		label.setBounds(331, 236, 93, 15);
		contentPane.add(label);
		
		button = new JButton("确认发布");
		button.setBounds(73, 232, 93, 23);
		contentPane.add(button);
		
		textArea = new JTextArea();
		textArea.setText("请在此处输入内容");
		textArea.setLineWrap(true);
		textArea.addKeyListener(this);
		textArea.addFocusListener(this);
		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(!update) {
					if(textArea.getText().equals("请在此处输入内容") || textArea.getText().equals("")) {
						JOptionPane.showMessageDialog(AnnouncementAdd.this, "请输入想要发布的内容", "错误", JOptionPane.ERROR_MESSAGE);
						return;
					}
					Announcement t = new Announcement();
					t.setInformation(textArea.getText());
					t.setRelease_time(new Date());
					t.setAlter_time(t.getRelease_time());
					
					if(as.save(t)) {
						JOptionPane.showMessageDialog(AnnouncementAdd.this, "发布成功", "提示", JOptionPane.INFORMATION_MESSAGE);
						admin.tableAdd();
						dispose();
					}
					else {
						JOptionPane.showMessageDialog(AnnouncementAdd.this, "发布失败", "错误", JOptionPane.ERROR_MESSAGE);
					}
				}
				else {
					Announcement t = new Announcement();
					t.setId(id);
					t.setInformation(textArea.getText());
					t.setAlter_time(new Date());
					if(as.update(t)) {
						JOptionPane.showMessageDialog(AnnouncementAdd.this, "修改成功", "提示", JOptionPane.INFORMATION_MESSAGE);
						admin.tableAdd();
						dispose();
					}else {
						JOptionPane.showMessageDialog(AnnouncementAdd.this, "修改失败", "错误", JOptionPane.ERROR_MESSAGE);
					}
				}
			}
		});
		
		scrollPane = new JScrollPane(textArea);
		scrollPane.setBounds(10, 0, 424, 210);
		contentPane.add(scrollPane);
		this.addWindowListener(new WindowAdapter() {
			
			@Override
			public void windowClosing(WindowEvent e) {
				if(update) {
					admin.esc(as.getById(id));
				}
			}
		});
	}
	
	public AnnouncementAdd(Admin_announcement admin) {
		this();
		this.admin = admin;
	}
	
	public AnnouncementAdd(Admin_announcement admin, String text, int id) {
		this(admin);
		update = true;
		button.setText("确认修改");
		textArea.setText(text);
		this.id = id;
		len();
	}

	@Override
	public void focusGained(FocusEvent e) {
		if(textArea.hasFocus() && textArea.getText().equals("请在此处输入内容")) {
			textArea.setText("");
		}
	}

	@Override
	public void focusLost(FocusEvent e) {
		if(textArea.hasFocus() && textArea.getText().equals("")) {
			textArea.setText("请在此处输入内容");
		}
	}
	
	public void len() {
		len = textArea.getText().length();
		label.setText(len+"/255");
	}

	@Override
	public void keyTyped(KeyEvent e) {
		JTextArea jta = (JTextArea)e.getSource();
		if(jta == textArea) {
			if(len >= 255) {
				e.consume();
				textArea.setText(textArea.getText().substring(0, 254));
			}
		}
	}

	@Override
	public void keyPressed(KeyEvent e) {
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		len();
	}
}
